package com.bd.scm.module.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bd.scm.dao.mapper.system.BdSysCostcenterMapper;
import com.bd.scm.domain.models.system.BdSysCostcenter;
import com.bd.scm.module.common.BaseServiceImpl;
import com.bd.scm.module.enums.CodeRuleEnum;
import com.bd.scm.module.system.service.BdSysCostcenterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 系统设置-成本中心表 服务实现类
 * </p>
 *
 * @author Mason
 * @since 2020-08-14
 */
@Service
public class BdSysCostcenterServiceImpl extends BaseServiceImpl<BdSysCostcenterMapper, BdSysCostcenter> implements BdSysCostcenterService {

    @Resource
    private BdSysCostcenterMapper bdSysCostcenterMapper;

    /**
     * 获取成本中心列表
     *
     * @param costcenterName
     * @return
     */
    @Override
    public List<BdSysCostcenter> getCostcenterList(String costcenterName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("costcenter_name", costcenterName);
        List<BdSysCostcenter> list = this.list(queryWrapper);
        return list;
    }

    /**
     * 新增或编辑.
     *
     * @param bdSysCostcenter
     * @return
     */
    @Override
    public String addOrEdit(BdSysCostcenter bdSysCostcenter) {
        //返回不为空，返回信息就是错误原因
        String msg = null;
        //判断成本中心名称是否重复
        Integer i = bdSysCostcenterMapper.getCountByName(bdSysCostcenter.getCostcenterName());
        msg = i > 0 ? "成本中心名称已存在！" : null;
        if (null == msg) {
            if (null != bdSysCostcenter.getId()){
                //编辑
                bdSysCostcenterMapper.editCostCenter(bdSysCostcenter);
            }else {
                //新增
                //获取id和bh
                Integer id = getMaxId(CodeRuleEnum.COSTCENTER);
                String bh = getNumber(CodeRuleEnum.COSTCENTER);
                bdSysCostcenter.setId(id);
                bdSysCostcenter.setBh(bh);
                bdSysCostcenterMapper.addCostCenter(bdSysCostcenter);
            }
        }
        return msg;
    }
    /**
     * 删除.
     *
     * @param bdSysCostcenter
     * @return
     */
    @Override
    public String deleteCostCenter(BdSysCostcenter bdSysCostcenter) {
        //返回不为空，返回信息就是错误原因
        String msg = null;
        //判断成本中心名称是否重复
        Integer i = bdSysCostcenterMapper.getCountOfDept(bdSysCostcenter);
        msg = i > 0 ? "成本中心已有部门绑定，无法删除！" : null;
        if (null == msg) {
            bdSysCostcenterMapper.deleteCostCenter(bdSysCostcenter);
        }
        return msg;
    }
}
